package cn.tangwei.org.controller;

import cn.tangwei.basic.utils.AjaxRusult;
import cn.tangwei.basic.utils.PageList;
import cn.tangwei.org.domain.Employee;
import cn.tangwei.org.query.EmployeeQuery;
import cn.tangwei.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  员工管理接口
 *      写接口的三要素：
 *           1.接口的请求地址是什么；
 *           2.请求参数是什么；
 *           3.返回值类型是什么。
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeContorller {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 新增或修改员工 put
     * 传对象就用@RequestBody
     * @param employee
     * @return
     */
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxRusult addOrupdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 删除员工 delete
     * 传值就用@PathVariable("id")再josn里面拿数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxRusult del(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxRusult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 查询一个员工 get
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    /**
     * 查询全部员工  get
     * @return
     */
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    /**
     * 分页加高级查询 post
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }
}
